package cn.aj.org.controller;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.ExcelUtil;
import cn.aj.basic.utils.PageListUtil;
import cn.aj.org.domain.Shop;
import cn.aj.org.dto.ShopAuditLogDto;
import cn.aj.org.dto.ShopSettlementAginDto;
import cn.aj.org.query.ShopQuery;
import cn.aj.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/22 0022 1:04
     * 批量删除
    */
    @PatchMapping(value="/{ids}")
    public AjaxResultUtil batchDelete(@PathVariable("ids") List<Long> ids){
        try {
            shopService.batchDelete(ids);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
     *
    */
    @PostMapping("/list")
    public PageListUtil<Shop> json(@RequestBody ShopQuery query) {
        return shopService.loadPage(query);
    }

    /**
    * @Title: settlement
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/19 0019 0:03
    * @Parameters: [shop]
    * @Return cn.aj.basic.utils.AjaxResult
     * 店铺入驻的接口
    */
    @PostMapping("/settlement")
    public AjaxResultUtil settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            // 捕捉自定义异常
            return AjaxResultUtil.getAjaxResult().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        return AjaxResultUtil.getAjaxResult().setSuccess(false).setMsg("系统繁忙,请稍后再试！");
         }
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/22 0022 16:52
     * 店铺驳回后再次入驻
    */
    @PostMapping("/settlementAgain")
    public AjaxResultUtil settlementAgain(@RequestBody ShopSettlementAginDto settlementAginDto) {
        try {
            shopService.settlementAgain(settlementAginDto);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            // 捕捉自定义异常
            return AjaxResultUtil.getAjaxResult().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().setSuccess(false).setMsg("系统繁忙,请稍后再试！");
        }
    }

    /**
    * @Title: auditPass
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/21 0021 19:20
    * @Parameters: [query]
    * @Return cn.aj.basic.utils.PageList<cn.aj.org.domain.Shop>
    *  审核通过业务
    */
    @PostMapping("/auditPass")
    public AjaxResultUtil auditPass(@RequestBody @Valid ShopAuditLogDto shopAuditLogDto) {
        try {
            shopService.auditPass(shopAuditLogDto);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return AjaxResultUtil.getAjaxResult().success();
    }
    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/21 0021 23:39
     * 驳回
    */
    @PostMapping("/auditReject")
    public AjaxResultUtil auditReject(@RequestBody @Valid ShopAuditLogDto shopAuditLogDto) {
        shopService.auditReject(shopAuditLogDto);
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/21 0021 23:39
     * 用户激活接口
    */
    @GetMapping("/activate/{id}")
    public AjaxResultUtil activate(@PathVariable("id") Long id) {
        shopService.activate(id);
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Title: activate
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/22 0022 16:09
    * @Parameters: [id]
    * @Return cn.aj.basic.utils.AjaxResultUtil
     * 表格导出
    */
    @GetMapping("/excelExport")
    public void excelExport(HttpServletResponse response) {
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops, "宠物之家商家入驻名单", "宠物之家", Shop.class,"店铺数据.xls", response);
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/22 0022 16:45
     * 报表导入
    */
    @PostMapping("/excelImport")
    public void excelImport(@RequestPart("file") MultipartFile file) {
        List<Shop> list = ExcelUtil.importExcel(file, 0, 2, Shop.class);
       list.forEach(System.out::println);
    }



}
